package shapes.shapes2D.distribution 
{
	import flash.geom.Point;
	import shapes.shapes2D.distribution.random.PRNG;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Circular
	{
		
		public var center:Point;
		public var radiusX:Number;
		public var radiusY:Number;
		private var points:Vector.<Point>;
		
		public function Circular( center:Point, radiusX:Number, radiusY:Number ) 
		{
			this.center = center;
			this.radiusX = radiusX;
			this.radiusY = radiusY;
		}
		
		public function distribute( count:uint, shuffle:Boolean = false, jitter:Number = 0 ):Vector.<Point> 
		{ 
			var p:Point;
			var i:int =  count, angleStep:Number, dx:Number, dy:Number;
			angleStep = ( Math.PI * 2 ) / i;
			
			points = new Vector.<Point>();
			while ( i-- )
			{
				
				p = new Point( 	center.x +	Math.cos( i * angleStep ) * radiusX, 
								center.y +	Math.sin( i * angleStep ) * radiusY );
				points.push( p );
				
			}
			
			if ( shuffle ) points.sort( this.shuffle );
			if ( jitter )jitterPoints( jitter );
			
			return points;
			
		}
		
		private function shuffle( a:Number, b:Number ):int
		{
			return ( Math.random() > .5 ) ? 1 : -1;
		}

		private function jitterPoints( radius:Number ):void
		{
			PRNG.seed = 0x80;
			var rad:Number;
			var angle:Number;
			for each( var p:Point in points )
			{
				
				rad = radius * Math.random();
				angle = ( 2 * Math.PI ) * Math.random();
				p.x += rad * Math.cos(angle);
				p.y += rad * Math.sin(angle);
				
			}
		}
		
	}

}